
// 定义proxy里的 handler方法
import { reactive } from './reactive';
import { isObject, hasOwnProperty, isEqual } from '../shared/utils';

// 方便以后扩展
const get = createGetter(),
      set = createSetter(),
      deleteProperty = createDeleteProperty();

function createGetter() {
  return function(target, key, receiver) {
    const res = Reflect.get(target, key, receiver);
    // console.log(res); // 如果返回的值也是对象则递归调用
    if(isObject(res)) return reactive(res);

    // todo ......

    return res;
  }
}

function createSetter() {
  return function(target, key, value, receiver) {
    
    const isKeyExist = hasOwnProperty(target, key), // 是否存在该属性，(判断是新增还是修改)
          oldValue = target[key],
          res = Reflect.set(target, key, value, receiver);

    if (!isKeyExist) {
      console.log('响应式新增：', value);

    } else if (!isEqual(value, oldValue)) {
      console.log('响应式修改：', key, value);

    }
    
    return res;
  }
}

function createDeleteProperty() {}


const mutableHandler = {
  get,
  set
}

export {
  mutableHandler
}